package com.itheima.reggie.controller;/**
 * 功能描述
 *
 * @author 唯硕
 * @date 2022/09/22  23:16
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *@ClassName ShoppingController
 *@Description TODO 购物车
 *@Author 唯硕
 *@Date 2022/9/22 23:16
 *@Version 1.0
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //查看当前购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //获取当前用户Id
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);

        return R.success(shoppingCartList);
    }

    //添加购物车
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {//要存入购物车的是菜品，
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        } else {
            //要存入购物车的是套餐 ，
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //select * from shopping_cart where user_id = ? and dish_id/setmeal_id=?
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);


        //查询当前菜品或者套餐是否在购物车中
        if (shoppingCart1 != null) {
            //如果已存在，就在原来数量上加一
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCart1);

        } else {
            //如果不存在，则添加到购物车，数量默认一
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shoppingCart1 = shoppingCart;
        }


        return R.success(shoppingCart1);
    }


    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        //delete from shopping_cart where user_id=?
        shoppingCartService.remove(queryWrapper);
        return R.success("购物车清空成功");

    }

    //sub方法减少菜品或套餐的数量
    @PostMapping("/sub")
    @Transactional
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        Long dishId = shoppingCart.getDishId();


        if (dishId != null) {//要删除购物车的是菜品，
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            ShoppingCart shoppingCart1= shoppingCartService.getOne(queryWrapper);
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            Integer latestNumber = shoppingCart1.getNumber();
            if (latestNumber > 0) {
                //对数据进行更新操作
                shoppingCartService.updateById(shoppingCart1);
            } else if (latestNumber == 0) {
                //如果购物车的数量减为0，菜品冲购物车中删除
                shoppingCartService.removeById(shoppingCart1.getId());

            } else if (latestNumber < 0) {
                return R.error("操作异常");

            }
            return R.success(shoppingCart1);
        }

        Long setmealId = shoppingCart.getSetmealId();
        if (setmealId != null) {//要删除购物车的是菜品，
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            ShoppingCart shoppingCart2 = shoppingCartService.getOne(queryWrapper);
            shoppingCart2.setNumber(shoppingCart2.getNumber()-1);
            Integer latestNumber = shoppingCart2.getNumber();
            if (latestNumber > 0) {
                //对数据进行更新操作
                shoppingCartService.updateById(shoppingCart2);
            } else if (latestNumber == 0) {
                //如果购物车的数量减为0，菜品冲购物车中删除
                shoppingCartService.removeById(shoppingCart2.getId());

            } else if (latestNumber < 0) {
                return R.error("操作异常");

            }
            return R.success(shoppingCart2);
        }
        return R.error("操作异常");
    }
}
